package cn.sms.org.controller;

import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.util.AjaxResult;
import cn.sms.basic.util.PageList;
import cn.sms.org.domain.Shop;
import cn.sms.org.query.ShopQuery;
import cn.sms.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService iShopService;

    @PostMapping("/settlement")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            iShopService.settledIn(shop);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }


    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return iShopService.queryById(id);
    }

    @GetMapping
    public List<Shop> queryAll(){
        return iShopService.queryAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iShopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }

    }

    @PutMapping
    public AjaxResult save(@RequestBody Shop shop){
        try {
            if (shop.getId()==null){
                iShopService.add(shop);
            }else {
                iShopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/list")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return iShopService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult batchRemoveShop(@RequestBody List<Long> ids){
        try {
            iShopService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

}
